package godis

import (
	"context"
	"fmt"
	"time"
)

type StringOperation struct {
	cxt context.Context
}

func NewStringOperation() *StringOperation {
	return &StringOperation{
		cxt: context.Background(),
	}
}



func (self *StringOperation) Set(key string, value interface{}, attrs ...*OperationAttr) *InterfaceResult {

	expr := OperationAttrs(attrs).Find(AttrExpire).UnWarpOr(0)
	nx := OperationAttrs(attrs).Find(AttrNX).UnWarpOr(nil)

	if nx != nil {
		fmt.Println("NX", nx)
		return NewInterfaceResult(Redis().SetNX(self.cxt, key, value, expr.(time.Duration)).Result())
	}

	xx := OperationAttrs(attrs).Find(AttrXX).UnWarpOr(nil)
	if xx != nil {
		fmt.Println("XX", xx)
		return NewInterfaceResult(Redis().SetXX(self.cxt, key, value, expr.(time.Duration)).Result())
	}
	return NewInterfaceResult(Redis().Set(self.cxt, key, value, expr.(time.Duration)).Result())
}

func (self *StringOperation) Get(key string) *StringResult {
	return NewStringResult(Redis().Get(self.cxt, key).Result())
}

func (self *StringOperation) MGet(keys ...string) *SliceResult {
	return NewSliceResult(Redis().MGet(self.cxt, keys...).Result())
}
